package com.cos.shiro.excepttion;

import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.NestedServletException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: COS
 * @time: 2021/11/22 17:26
 * @description:
 */
public class ShiroExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        System.out.println("出错啦:"+ex.getMessage());
        ModelAndView mv= new ModelAndView();
        if (ex instanceof UnknownAccountException) {
            mv.setViewName("redirect:/login.jsp");
        }else if(ex instanceof NestedServletException){
            mv.setViewName("redirect:/error.jsp");
        }else{
            mv.setViewName("redirect:/login.jsp");
        }
        return mv;
    }
}
